import {removeToken,removeUserId,removeSchoolInfo,removeUserInfo} from "@/utils/author";
import {userLogin} from "@/api/user/index"

export function login() {
  uni.login({
    provider: "weixin",
    success: function (loginRes) {
      // 获取用户信息
      uni.getUserInfo({
        provider: "weixin",
        success: function (infoRes) {
          //使用获取的信息去后台服务器交换token
          userLogin(infoRes.userInfo).then((result) => {
            //存储Token
            const { token } = result;
            const { openId, avatarUrl, nickName, school_id } = result.data;
            setToken(token);
            setUserInfo(JSON.stringify(result.data));
            setUserId(openId);
          });
        },
        fail: function () {
          console.log("获取用户信息失败");
        },
      });
    },
  });
}

/**
 * 前端登出 清除缓存中的数据
 */
export function logoutFront(){
  removeToken();
  removeUserId();
  removeSchoolInfo();
  removeUserInfo();
}

/**
 * 后端登出
 * @param {String}} open_id 
 */
export function logout(open_id){

}